package com.relytosoft.mizar.media.system.services.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.relytosoft.dubhe.common.core.utils.TreeBuildUtils;
import com.relytosoft.dubhe.common.satoken.entity.CurrentUser;
import com.relytosoft.mizar.media.system.api.entity.SysDistrict;
import com.relytosoft.mizar.media.system.api.pojo.vo.DistrictSyncVo;
import com.relytosoft.mizar.media.system.api.pojo.dto.DistrictFeignDto;
import com.relytosoft.mizar.media.system.api.pojo.vo.SysDistrictVo;
import com.relytosoft.mizar.media.system.services.mapper.SysDistrictMapper;
import com.relytosoft.mizar.media.system.services.service.SysDistrictService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 区域
 *
 * @Author SunLi
 * @Date: 2022/4/7 14:21
 * @Version 1.0
 */
@Service
public class SysDistrictServiceImpl extends ServiceImpl<SysDistrictMapper,SysDistrict> implements SysDistrictService {


    @Override
    public int updateSysDistrictStatus(int id,int status){
        return baseMapper.update(null,new LambdaUpdateWrapper<SysDistrict>().set(SysDistrict::getStatus,status).set(SysDistrict::getUpdatedId, CurrentUser.getCurrentUserId()).set(SysDistrict::getUpdatedBy, CurrentUser.getCurrentUserFullName()).set(SysDistrict::getUpdatedTime, LocalDateTime.now()).eq(SysDistrict::getId,id));
    }
    @Override
    public List<SysDistrictVo> selectSysDistrictVoList(SysDistrict sysDistrict) {
        return baseMapper.selectSysDistrictVoList(sysDistrict);
    }

    @Override
    public List<SysDistrict> selectSysDistrictList(String name) {
        LambdaQueryWrapper<SysDistrict> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysDistrict::getStatus,1);
        if(!StrUtil.isEmpty(name)){
            wrapper.like(SysDistrict::getName,name);
        }
        wrapper.orderByAsc(SysDistrict::getParentId).orderByDesc(SysDistrict::getSort);
        return this.list(wrapper);
    }


    @Override
    public List<Tree<Integer>> buildSysDistrictTreeSelect(List<SysDistrict> districts) {
        if (CollUtil.isEmpty(districts)) {
            return CollUtil.newArrayList();
        }
        return TreeBuildUtils.build(districts, (district, tree) ->
                tree.setId(district.getId())
                        .setParentId(district.getParentId())
                        .setName(district.getName())
                        .setWeight(-district.getSort()));
    }


    @Override
    public boolean checkCodeUnique(SysDistrict sysDistrict) {
        int id = ObjectUtil.isNull(sysDistrict.getId()) ? -1 : sysDistrict.getId();
        long count = count(new LambdaQueryWrapper<SysDistrict>()
                .eq(SysDistrict::getCode, sysDistrict.getCode())
                .ne(SysDistrict::getId, id));
        return count <= 0;
    }

    @Override
    public boolean checkNameUnique(SysDistrict sysDistrict) {
        int id = ObjectUtil.isNull(sysDistrict.getId()) ? -1 : sysDistrict.getId();
        long count = count(new LambdaQueryWrapper<SysDistrict>()
                .eq(SysDistrict::getName, sysDistrict.getName())
                .eq(SysDistrict::getParentId, sysDistrict.getParentId())
                .ne(SysDistrict::getId, id));
        return count <= 0;
    }

    @Override
    public boolean canDisable(int id) {
        if(id<=0){
            return false;
        }
        long count = count(new LambdaQueryWrapper<SysDistrict>()
                .eq(SysDistrict::getParentId, id)
                .eq(SysDistrict::getStatus, 1) );
        return count <= 0;
    }

    @Override
    public List<DistrictSyncVo> getDistrictSyncList(String storeType) {
        return baseMapper.getDistrictSyncList(storeType);
    }

    @Override
    public SysDistrictVo getSysDistrictVo(int id) {
        return baseMapper.getSysDistrictVo(id);
    }

    @Override
    public long countBySecurityLevel(int securityLevelId) {
        return this.count(new LambdaQueryWrapper<SysDistrict>()
                .eq(SysDistrict::getSecurityLevelId,securityLevelId));
    }

    @Override
    public List<DistrictFeignDto> selectDistrictFeignList(String storeType) {
        return baseMapper.getDistrictFeignList(storeType);
    }
}